package com.test.mchelepesa.activity_fragment_rice.loan_rice;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.test.mchelepesa.activity_fragment_rice.base_rice.BaseLoanActivity_rice;
import com.test.mchelepesa.bean_rice.StaticConfig_rice;
import com.test.mchelepesa.bean_rice.user_info_rice.LoanApplyDetailInfo_rice;
import com.test.mchelepesa.util_rice.StringUtil_rice;
import com.test.mchelepesa.util_rice.http_rice.HttpManager_rice;
import com.test.mchelepesa.view_rice.widget_rice.TitleView_rice;
import com.test.mchelepesa.R;

import java.util.ArrayList;
import java.util.LinkedHashMap;


public class LoanApplyActivity_rice extends BaseLoanActivity_rice {

    private TextView loanAmountTv;
    private TextView loanTermTv;
    private LinearLayout groupLayout;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_loan_apply_rice);
        init();
    }

    private void init() {
        TitleView_rice titleView = findViewById(R.id.activity_loan_apply_view_title);
        setBackBtn_rice(titleView.getBackIb());

        loanAmountTv = findViewById(R.id.activity_loan_apply_tv_loan_amount);
        loanTermTv = findViewById(R.id.activity_loan_apply_tv_loan_term);
        groupLayout = findViewById(R.id.activity_loan_apply_group);
        Button applyBn = findViewById(R.id.activity_loan_apply_btn_apply);

        initLicense(
                findViewById(R.id.activity_loan_apply_view_license),
                findViewById(R.id.activity_loan_apply_license_logo),
                findViewById(R.id.activity_loan_apply_license_company_name)
        );
        applyBn.setOnClickListener(v -> {
            HttpManager_rice.getInstance().saveUserBuriedPointAndUpload((LoanApplyActivity_rice.class.getSimpleName() + "_CLICK").toUpperCase());
            HttpManager_rice.getInstance().loanApply();
        });

        // 这一段
//        ThreadUtil_rice.getInstance().runOnChildThread(() -> {
//            try {
//                Thread.sleep(300);
//            } catch (InterruptedException e) {
//                StringUtil_rice.printException(e);
//            }
//            HttpManager_rice.getInstance().loanApply();
//        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        HttpManager_rice.getInstance().requestLoanApplyDetail();
    }

    @SuppressLint("SetTextI18n")
    public void onLoanApplyDetailResponse() {

        //货币单位
        String moneyUnit = StringUtil_rice.getSafeString(StaticConfig_rice.getMoneyUnit(LoanApplyDetailInfo_rice.getInstance().getCurrency())).toUpperCase();

        loanAmountTv.setText(moneyUnit + " " + LoanApplyDetailInfo_rice.getInstance().getAmount());
        loanTermTv.setText(LoanApplyDetailInfo_rice.getInstance().getDays() + " " + getString(R.string.word_days));

        ArrayList<LinkedHashMap<String, String>> detailsList = LoanApplyDetailInfo_rice.getInstance().getDetailItemList();

        if (detailsList == null) return;

        View detailsView = null;
        LinearLayout detailsGroup = null;

        for (int i = 0; i < detailsList.size(); i++) {

            LinkedHashMap<String, String> detailMap = detailsList.get(i);

            if (detailMap != null) {

                String title = StringUtil_rice.getSafeString(detailMap.get("title"));
                String showName = StringUtil_rice.getSafeString(detailMap.get("showName"));
                String showDetail = StringUtil_rice.getSafeString(detailMap.get("showDetail"));

                if ("YES".equalsIgnoreCase(title)) {

                    if (detailsView != null) groupLayout.addView(detailsView);

                    detailsView = View.inflate(this, R.layout.view_apply_details, null);
                    TextView titleNameTv = detailsView.findViewById(R.id.view_apply_details_tv_title_name);
                    TextView titleValueTv = detailsView.findViewById(R.id.view_apply_details_tv_title_value);
                    detailsGroup = detailsView.findViewById(R.id.view_apply_details_group);

                    titleNameTv.setText(showName);
                    titleValueTv.setText(showDetail);

                } else if (detailsGroup != null) {

                    View item = View.inflate(this, R.layout.item_apply_details, null);
                    TextView nameTv = item.findViewById(R.id.item_apply_details_name);
                    TextView valueTv = item.findViewById(R.id.item_apply_details_value);

                    nameTv.setText(showName);
                    valueTv.setText(showDetail);

                    detailsGroup.addView(item);

                }

            }

        }

        if (detailsView != null) groupLayout.addView(detailsView);

    }

    public void onLoanApplyResponse() {
        HttpManager_rice.getInstance().requestUserProcess();
    }

}